//
//  NetWorkTool.swift
//  weibo
//
//  Created by wang_h on 2017/5/31.
//  Copyright © 2017年 wang_h. All rights reserved.
//
import AFNetworking

enum RequestType : String {
    case GET = "GET"
    case POST = "POST"
}

class NetWorkTools: AFHTTPSessionManager {
    //let是线程安全的
    static let shareInstance : NetWorkTools = {
        let tools = NetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
}

//MARK:- 封装网络请求
extension NetWorkTools {
    
    func request(methodType : RequestType , urlString : String, parameters : [String : AnyObject] , finished : @escaping (_ result : AnyObject?, _ error : Error?) -> ()) {
        
        //1.定义成功地回调函数
        let successCallBack = { (task : URLSessionDataTask, result : Any?) in
            finished(result as AnyObject, nil)
        }
        //2.定义失败的回调函数
        let failCallBack = { (task : URLSessionDataTask?, error : Error) in
            finished(nil, error)
        }
        
        //3.发送网络请求
        if methodType == .GET {
            get(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failCallBack)
        }else{
            post(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failCallBack)
        }
    }
}

//MARK:- 请求AccessToken
extension NetWorkTools {
    func loadAccessToken(code : String, finished : @escaping (_ result : [String : AnyObject]?, _ error : Error?) -> ()) {
        //1.获取请求的URLString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        //2.获取请求的参数
        let parameters = ["client_id" : app_key, "client_secret" : app_secret, "grant_type" : "authorization_code", "redirect_uri" : redirect_url , "code" : code]
        
        //3.发送网络请求
        request(methodType: .POST, urlString: urlString, parameters: parameters as [String : AnyObject]) { (result, error) in
            finished(result as? [String : AnyObject], error)
        }
    }
}

//MARK:- 请求用户信息
extension NetWorkTools {
    func loadUserInfo(access_token : String, uid : String, finished : @escaping (_ result : [String : AnyObject]?, _ error : Error?) -> ()) {
        let urlString = "https://api.weibo.com/2/users/show.json"
        let parameters = ["access_token":access_token,"uid" : uid]
        
        request(methodType: .GET, urlString: urlString, parameters: parameters as [String : AnyObject]) { (result, error) in
            finished(result as? [String : AnyObject], error)
        }
    }
}

//MARK:- 请求首页数据
extension NetWorkTools {
    func loadStatuses(finished : @escaping (_ result : [[String : AnyObject]]?, _ error : Error?) -> ()) {
        
        let urlString = "https://api.weibo.com/2/statuses/public_timeline.json"
        let  parameters = ["access_token" : (UserAccountViewModel.shareInstance.account?.access_token)!]
        request(methodType: .GET, urlString: urlString, parameters: parameters as [String : AnyObject]) { (result, error) in
            
            guard let resultDict = result as? [String : AnyObject] else {
                finished(nil, error)
                return
            }
            finished(resultDict["statuses"] as? [[String : AnyObject]], error)
        }
    }
}




















